import java.io.IOException;
import java.util.ArrayList;


public class GameStats {

	public static int numOfFreeTurns(int player, ArrayList<MancalaGameState> states) {
		int numOfFreeTurns = 0;
		if (states.size() > 1) {
			for (int i = 1; i < states.size() - 1; i++) { // size - 1 because last state is not a played state.
				if (states.get(i).CurrentPlayer() == player &&
						//If the player for the previous state is the same as the current state,
						//player must have received a free turn.
						states.get(i).CurrentPlayer() == states.get(i - 1).CurrentPlayer()) {
					
					numOfFreeTurns++;
				}
			}
		}
		return numOfFreeTurns;
	}
	
	public static int numOfMoves(int player, ArrayList<MancalaGameState> states) {
		int numOfMoves = 0;
		for (int i = 0; i < states.size() - 2; i++) { // size - 2 because in last two states no one moves.
			if (states.get(i).CurrentPlayer() == player) {
				numOfMoves++;
			}
		}
		return numOfMoves;
	}

	//Returns 1 if identified player won, -1 if lost, 0 if tie. Other player = 1 - currentPlayer.
	public static int winLoseTie(int player, ArrayList<MancalaGameState> states) throws IOException {
		if (states == null || states.size() == 0)
			throw new IOException("No states found");
		
		if (states.get(states.size() - 1).getScore(player) > states.get(states.size() - 1).getScore(1 - player))
			return 1;
		else if (states.get(states.size() - 1).getScore(player) < states.get(states.size() - 1).getScore(1 - player))
			return -1;
		else
			return 0;
	}
	
	public static boolean wentFirst(int player, ArrayList<MancalaGameState> states) throws IOException {
		if (states == null || states.size() == 0)
			throw new IOException("No states found");
		else 
			return states.get(1).CurrentPlayer() == player;
	}
	
	/* Returns 0 if no capture was made or if not player's turn. 
	 * Returns the number of stones captured if a capture was made.
	 * The state number is the one where the player made their move.
	 */
	public static int captureAmt(int player, ArrayList<MancalaGameState> states, int stateNum) throws IOException {
		if (states == null || states.size() == 0)
			throw new IOException("No states found");
		if (stateNum < 0 && stateNum > states.size() - 3) { // size - 3 because last 2 states are not played.
			throw new IOException("Invalid state number. It must be between 0 and " + (states.size() - 1));
		}
		
		int captureAmt = 0;
		if (states.size() > 1 && states.get(stateNum).CurrentPlayer() == player) {
			for (int i = 0; i < 6; i++) { //6 is the number of columns
				captureAmt = states.get(stateNum).stonesAt(1 - player, i) - states.get(stateNum + 1).stonesAt(1 - player, i);
				if (captureAmt > 0) {
					return captureAmt;
				}
			}
		}
		return 0;
	}
	
	public static int greatestCapture(int player, ArrayList<MancalaGameState> states) throws IOException {
		if (states == null || states.size() == 0)
			throw new IOException("No states found");

		int greatestCapture = 0, currentCapture = 0;
		for (int i = 1; i < states.size() - 1; i++) {
			currentCapture = captureAmt(player, states, i);
			if (currentCapture > greatestCapture) {
				greatestCapture = currentCapture;
			}
		}
		return greatestCapture;
	}
	
	/* Returns 0 if no capture was made or if not player's turn. 
	 * Returns the number of stones captured if a capture was made.
	 * The state number is the one where the player made their move.
	 */
	public static int numOfCaptures(int player, ArrayList<MancalaGameState> states) throws IOException {
		if (states == null || states.size() == 0)
			throw new IOException("No states found");
		
		int numOfCaptures = 0;
		
			for (int i = 0; i < states.size() - 2; i++) {
				if (states.get(i).CurrentPlayer() == player &&
						captureAmt(player, states, i) > 0)
					numOfCaptures++;
		}
		return numOfCaptures;
	}
	
	public static void printStats(ArrayList<MancalaGameState> states, int i) throws IOException {
		if (states == null || states.size() == 0)
			throw new IOException("No states found");
		
		
			System.out.print("Player " + i);
			if (wentFirst(i, states))
				System.out.println(" went first.");
			else
				System.out.println(" went second");
			
			System.out.print("Result: ");
			if (winLoseTie(i, states) == 0)
				System.out.println("Tie");
			else if (winLoseTie(i, states) == 1)
				System.out.println("Win");
			else
				System.out.println("Lose");
			System.out.println("Score: " + states.get(states.size() - 1).getScore(i));
//			System.out.println("Number of Captures: " + numOfCaptures(i, states));
//			System.out.println("Greatest Capture: " + greatestCapture(i, states));
//			System.out.println("Number of Free Turns: " + numOfFreeTurns(i, states));
			System.out.println("Number of Moves: " + numOfMoves(i, states));
		
		
	}
}
